## distribution data preparation ####
library(sp)
library(dismo)
## Loading required package: raster
library(raster)
library(gbm)
## Loaded gbm 2.1.5
library(maptools)
## Checking rgeos availability: FALSE
##      Note: when rgeos is not available, polygon geometry     computations in maptools depend on gpclib,
##      which has a restricted licence. It is disabled by default;
##      to enable gpclib, type gpclibPermit()
library(rgdal)
## rgdal: version: 1.4-6, (SVN revision 841)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
##  Path to GDAL shared files: C:/Users/ich/Documents/R/win-library/3.6/rgdal/gdal
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: C:/Users/ich/Documents/R/win-library/3.6/rgdal/proj
##  Linking to sp version: 1.3-1
library(sp)
library(shapefiles)
## Loading required package: foreign
## 
## Attaching package: 'shapefiles'
## The following objects are masked from 'package:foreign':
## 
##     read.dbf, write.dbf
# DATA distribution

vignette('sdm', 'dismo')

distribution_shape <- read.shp("C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/distribution_Pakistan_all_test.shp")

distribution_dataframe <- as.data.frame(distribution_shape)

write.csv(distribution_shape, file = "C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/distribution_shape_csv.csv")

occurrence_csv <- paste0(system.file(package="dismo"), "C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/distribution_shape_csv.csv")
head(occurrence_csv)
## [1] "C:/Users/ich/Documents/R/win-library/3.6/dismoC:/Users/ich/Desktop/UNI/räumliche_Vorhersage/distribution_shape_csv.csv"
occurrence <- read.csv("C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/distribution_shape_csv.csv")
head(occurrence)
##   X shp.record    shp.x    shp.y shp.shape.type header.file.code
## 1 1          1 72.07099 35.99492              1             9994
## 2 2          2 71.99009 35.15735              1             9994
## 3 3          3 73.39397 34.45065              1             9994
## 4 4          4 73.16466 33.82338              1             9994
## 5 5          5 73.16823 34.13390              1             9994
## 6 6          6 73.41688 34.00422              1             9994
##   header.file.length header.file.version header.shape.type header.xmin
## 1              82230                1000                 1    62.23056
## 2              82230                1000                 1    62.23056
## 3              82230                1000                 1    62.23056
## 4              82230                1000                 1    62.23056
## 5              82230                1000                 1    62.23056
## 6              82230                1000                 1    62.23056
##   header.ymin header.xmax header.ymax header.zmin header.zmax header.mmin
## 1    24.13072    77.77783    37.11705           0           0           0
## 2    24.13072    77.77783    37.11705           0           0           0
## 3    24.13072    77.77783    37.11705           0           0           0
## 4    24.13072    77.77783    37.11705           0           0           0
## 5    24.13072    77.77783    37.11705           0           0           0
## 6    24.13072    77.77783    37.11705           0           0           0
##   header.mmax
## 1           0
## 2           0
## 3           0
## 4           0
## 5           0
## 6           0
occurrence_lonlat <- occurrence [,3:4]
head(occurrence_lonlat)
##      shp.x    shp.y
## 1 72.07099 35.99492
## 2 71.99009 35.15735
## 3 73.39397 34.45065
## 4 73.16466 33.82338
## 5 73.16823 34.13390
## 6 73.41688 34.00422
# dataframe distribution data

dataframe_occurrence_record_lonlat <- occurrence [,2:4]
head(dataframe_occurrence_record_lonlat)
##   shp.record    shp.x    shp.y
## 1          1 72.07099 35.99492
## 2          2 71.99009 35.15735
## 3          3 73.39397 34.45065
## 4          4 73.16466 33.82338
## 5          5 73.16823 34.13390
## 6          6 73.41688 34.00422
write.csv(dataframe_occurrence_record_lonlat, "C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/dataframe_occurrence_record_lonlat.csv")
head(dataframe_occurrence_record_lonlat)
##   shp.record    shp.x    shp.y
## 1          1 72.07099 35.99492
## 2          2 71.99009 35.15735
## 3          3 73.39397 34.45065
## 4          4 73.16466 33.82338
## 5          5 73.16823 34.13390
## 6          6 73.41688 34.00422
## species data preparation ####
# read in species_csv

species_subspecies <- read.csv("C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/distribution_Pakistan_all_species.csv")

species_subspecies_cut <- species_subspecies [,0:2]
head(species_subspecies_cut)
##           species.C.80 subspecies.C.80
## 1      Actinor_radians                
## 2      Actinor_radians                
## 3      Actinor_radians                
## 4 Aeromachus_stigmatus                
## 5 Aeromachus_stigmatus                
## 6 Aeromachus_stigmatus
## combine species + distribution + cut (subset) for only 1 species (--> Gegenes_nostrodamus) ####

species_distribution_combined <- data.frame(dataframe_occurrence_record_lonlat, species_subspecies_cut)
head(species_distribution_combined)
##   shp.record    shp.x    shp.y         species.C.80 subspecies.C.80
## 1          1 72.07099 35.99492      Actinor_radians                
## 2          2 71.99009 35.15735      Actinor_radians                
## 3          3 73.39397 34.45065      Actinor_radians                
## 4          4 73.16466 33.82338 Aeromachus_stigmatus                
## 5          5 73.16823 34.13390 Aeromachus_stigmatus                
## 6          6 73.41688 34.00422 Aeromachus_stigmatus
# subset for species: Gegenes_nostrodamus

subset_Gegenes_nostrodamus <- subset(species_distribution_combined, species.C.80=="Gegenes_nostrodamus")
subset_Gegenes_nostrodamus
##    shp.record    shp.x    shp.y        species.C.80 subspecies.C.80
## 53         53 74.75619 35.27489 Gegenes_nostrodamus     nostrodamus
## 54         54 74.25131 35.28893 Gegenes_nostrodamus     nostrodamus
## 55         55 72.38800 35.94713 Gegenes_nostrodamus     nostrodamus
## 56         56 71.90671 35.83228 Gegenes_nostrodamus     nostrodamus
## 57         57 72.02838 35.21013 Gegenes_nostrodamus     nostrodamus
## 58         58 71.88014 34.80043 Gegenes_nostrodamus     nostrodamus
## 59         59 71.90288 34.46425 Gegenes_nostrodamus     nostrodamus
## 60         60 73.43660 33.89194 Gegenes_nostrodamus     nostrodamus
## 61         61 73.09218 34.16677 Gegenes_nostrodamus     nostrodamus
## 62         62 72.90301 33.97403 Gegenes_nostrodamus     nostrodamus
## 63         63 73.09396 33.89551 Gegenes_nostrodamus     nostrodamus
## 64         64 72.61379 33.70444 Gegenes_nostrodamus     nostrodamus
## 65         65 72.81188 33.69195 Gegenes_nostrodamus     nostrodamus
## 66         66 74.17204 31.62093 Gegenes_nostrodamus     nostrodamus
## 67         67 73.99001 31.47994 Gegenes_nostrodamus     nostrodamus
## 68         68 66.75859 30.43480 Gegenes_nostrodamus     nostrodamus
## 69         69 66.72290 30.06361 Gegenes_nostrodamus     nostrodamus
## 70         70 67.00843 24.94896 Gegenes_nostrodamus     nostrodamus
## 71         71 73.06086 33.49902 Gegenes_nostrodamus         karsana
subset_Gegenes_nostrodamus_lat_lon <- subset_Gegenes_nostrodamus[,2:3]
subset_Gegenes_nostrodamus_lat_lon
##       shp.x    shp.y
## 53 74.75619 35.27489
## 54 74.25131 35.28893
## 55 72.38800 35.94713
## 56 71.90671 35.83228
## 57 72.02838 35.21013
## 58 71.88014 34.80043
## 59 71.90288 34.46425
## 60 73.43660 33.89194
## 61 73.09218 34.16677
## 62 72.90301 33.97403
## 63 73.09396 33.89551
## 64 72.61379 33.70444
## 65 72.81188 33.69195
## 66 74.17204 31.62093
## 67 73.99001 31.47994
## 68 66.75859 30.43480
## 69 66.72290 30.06361
## 70 67.00843 24.94896
## 71 73.06086 33.49902
#final presence data

p_a <- 1

final_presence <- cbind(subset_Gegenes_nostrodamus_lat_lon, p_a)
final_presence
##       shp.x    shp.y p_a
## 53 74.75619 35.27489   1
## 54 74.25131 35.28893   1
## 55 72.38800 35.94713   1
## 56 71.90671 35.83228   1
## 57 72.02838 35.21013   1
## 58 71.88014 34.80043   1
## 59 71.90288 34.46425   1
## 60 73.43660 33.89194   1
## 61 73.09218 34.16677   1
## 62 72.90301 33.97403   1
## 63 73.09396 33.89551   1
## 64 72.61379 33.70444   1
## 65 72.81188 33.69195   1
## 66 74.17204 31.62093   1
## 67 73.99001 31.47994   1
## 68 66.75859 30.43480   1
## 69 66.72290 30.06361   1
## 70 67.00843 24.94896   1
## 71 73.06086 33.49902   1
## chelsa data preparation ####

#merge

merged_chelsa_annual_mean_temp_path <- "C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/annual_mean_temperature/"
merged_chelsa_annual_mean_temp_path
## [1] "C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/annual_mean_temperature/"
merged_chelsa_annual_mean_temp_files <- list.files("C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/annual_mean_temperature/")
merged_chelsa_annual_mean_temp_files
##  [1] "CHELSA_temp10_01_1979-2013_V1.2_land.tif"    
##  [2] "CHELSA_temp10_02_1979-2013_V1.2_land.tif"    
##  [3] "CHELSA_temp10_03_1979-2013_V1.2_land.tif"    
##  [4] "CHELSA_temp10_04_1979-2013_V1.2_land.tif"    
##  [5] "CHELSA_temp10_05_1979-2013_V1.2_land.tif"    
##  [6] "CHELSA_temp10_06_1979-2013_V1.2_land.tif"    
##  [7] "CHELSA_temp10_07_1979-2013_V1.2_land.tif"    
##  [8] "CHELSA_temp10_08_1979-2013_V1.2_land (1).tif"
##  [9] "CHELSA_temp10_09_1979-2013_V1.2_land.tif"    
## [10] "CHELSA_temp10_10_1979-2013_V1.2_land.tif"    
## [11] "CHELSA_temp10_11_1979-2013_V1.2_land.tif"    
## [12] "CHELSA_temp10_12_1979-2013_V1.2_land.tif"    
## [13] "merged"
raster_list <- list(merged_chelsa_annual_mean_temp_files) 
raster_list
## [[1]]
##  [1] "CHELSA_temp10_01_1979-2013_V1.2_land.tif"    
##  [2] "CHELSA_temp10_02_1979-2013_V1.2_land.tif"    
##  [3] "CHELSA_temp10_03_1979-2013_V1.2_land.tif"    
##  [4] "CHELSA_temp10_04_1979-2013_V1.2_land.tif"    
##  [5] "CHELSA_temp10_05_1979-2013_V1.2_land.tif"    
##  [6] "CHELSA_temp10_06_1979-2013_V1.2_land.tif"    
##  [7] "CHELSA_temp10_07_1979-2013_V1.2_land.tif"    
##  [8] "CHELSA_temp10_08_1979-2013_V1.2_land (1).tif"
##  [9] "CHELSA_temp10_09_1979-2013_V1.2_land.tif"    
## [10] "CHELSA_temp10_10_1979-2013_V1.2_land.tif"    
## [11] "CHELSA_temp10_11_1979-2013_V1.2_land.tif"    
## [12] "CHELSA_temp10_12_1979-2013_V1.2_land.tif"    
## [13] "merged"
for(i in 1:12)
{
  tilesloop <- paste0(merged_chelsa_annual_mean_temp_path, merged_chelsa_annual_mean_temp_files[i])
  tilesloop       
  raster_list[[i]] <- raster(tilesloop)
  rasterliste <- raster_list[[i]]
  rasterliste
}

raster_list$filename     <- "C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/annual_mean_temperature/merged/merged_chelsa_annual_mean_temp.tif"
exporttif <- raster_list$filename 
exporttif
## [1] "C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/annual_mean_temperature/merged/merged_chelsa_annual_mean_temp.tif"
raster_list$overwrite    <- TRUE


all_merged_chelsa_annual_mean_temp_raster <- do.call(raster::merge, raster_list, quote=TRUE)
all_merged_chelsa_annual_mean_temp_raster
## class      : RasterLayer 
## dimensions : 20880, 43200, 902016000  (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333  (x, y)
## extent     : -180.0001, 179.9999, -90.00014, 83.99986  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## source     : C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/annual_mean_temperature/merged/merged_chelsa_annual_mean_temp.tif 
## names      : merged_chelsa_annual_mean_temp 
## values     : -457, 345  (min, max)
plot(all_merged_chelsa_annual_mean_temp_raster, main="all_merged chelsa annual mean temperature")

chelsa_annual_global_prec <- 
  raster("C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/annual_global_prec/CHELSA_bio10_12.tif")
chelsa_prec_warmest_quarter <- 
  raster("C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/prec_warmest_quarter/CHELSA_bio10_18.tif")
chelsa_warmest_quarter_mean_temp <- 
  raster("C:/Users/ich/Desktop/UNI/räumliche_Vorhersage/Pakistan_alles/chelsa/warmest_quarter_mean_t_global/CHELSA_bio10_10.tif")

plot(all_merged_chelsa_annual_mean_temp_raster, main="all_merged chelsa annual mean temperature")

plot(chelsa_annual_global_prec, main="chelsa annual global prec")

plot(chelsa_prec_warmest_quarter, main="chelsa prec warmest quarter")

plot(chelsa_warmest_quarter_mean_temp, main="chelsa warmest quarter mean temperature")

# crop onto pakistan extent

extend_pakistan <- extent(c(58, 83, 23, 38))

cropped_chelsa_annual_mean_temp_raster <- crop(all_merged_chelsa_annual_mean_temp_raster, extend_pakistan)
cropped_chelsa_annual_mean_temp_raster
## class      : RasterLayer 
## dimensions : 1800, 3000, 5400000  (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333  (x, y)
## extent     : 57.99986, 82.99986, 22.99986, 37.99986  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## source     : C:/Users/ich/AppData/Local/Temp/Rtmp6nsTNH/raster/r_tmp_2020-01-19_000355_16336_94256.grd 
## names      : merged_chelsa_annual_mean_temp 
## values     : -341, 224  (min, max)
plot(cropped_chelsa_annual_mean_temp_raster)

cropped_chelsa_annual_global_prec <- crop(chelsa_annual_global_prec,extend_pakistan)
cropped_chelsa_annual_global_prec
## class      : RasterLayer 
## dimensions : 1800, 3000, 5400000  (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333  (x, y)
## extent     : 57.99986, 82.99986, 22.99986, 37.99986  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## source     : C:/Users/ich/AppData/Local/Temp/Rtmp6nsTNH/raster/r_tmp_2020-01-19_000359_16336_12575.grd 
## names      : CHELSA_bio10_12 
## values     : 14, 3406  (min, max)
plot(cropped_chelsa_annual_global_prec)

cropped_chelsa_prec_warmest_quarter <- crop(chelsa_prec_warmest_quarter, extend_pakistan)
cropped_chelsa_prec_warmest_quarter
## class      : RasterLayer 
## dimensions : 1800, 3000, 5400000  (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333  (x, y)
## extent     : 57.99986, 82.99986, 22.99986, 37.99986  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## source     : C:/Users/ich/AppData/Local/Temp/Rtmp6nsTNH/raster/r_tmp_2020-01-19_000403_16336_25924.grd 
## names      : CHELSA_bio10_18 
## values     : 0, 3221  (min, max)
plot(cropped_chelsa_prec_warmest_quarter)

cropped_chelsa_warmest_quarter_mean_temp <- crop(chelsa_warmest_quarter_mean_temp, extend_pakistan,)
cropped_chelsa_warmest_quarter_mean_temp
## class      : RasterLayer 
## dimensions : 1800, 3000, 5400000  (nrow, ncol, ncell)
## resolution : 0.008333333, 0.008333333  (x, y)
## extent     : 57.99986, 82.99986, 22.99986, 37.99986  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## source     : C:/Users/ich/AppData/Local/Temp/Rtmp6nsTNH/raster/r_tmp_2020-01-19_000407_16336_62071.grd 
## names      : CHELSA_bio10_10 
## values     : -110, 402  (min, max)
plot(cropped_chelsa_warmest_quarter_mean_temp)

# rasterstack

rasterstack_pakistan_chelsa <- raster::stack(cropped_chelsa_annual_global_prec, 
                                             cropped_chelsa_annual_mean_temp_raster, 
                                             cropped_chelsa_prec_warmest_quarter, 
                                             cropped_chelsa_warmest_quarter_mean_temp)
rasterstack_pakistan_chelsa
## class      : RasterStack 
## dimensions : 1800, 3000, 5400000, 4  (nrow, ncol, ncell, nlayers)
## resolution : 0.008333333, 0.008333333  (x, y)
## extent     : 57.99986, 82.99986, 22.99986, 37.99986  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## names      : CHELSA_bio10_12, merged_chelsa_annual_mean_temp, CHELSA_bio10_18, CHELSA_bio10_10 
## min values :              14,                           -341,               0,            -110 
## max values :            3406,                            224,            3221,             402
plot(rasterstack_pakistan_chelsa)

# dataframe chelsa

pakistan_chelsa_dataframe <- as.data.frame(rasterstack_pakistan_chelsa)
head(pakistan_chelsa_dataframe)
##   CHELSA_bio10_12 merged_chelsa_annual_mean_temp CHELSA_bio10_18
## 1             241                             30              22
## 2             229                             32              21
## 3             228                             29              18
## 4             245                             28              18
## 5             281                             22              19
## 6             322                              9              22
##   CHELSA_bio10_10
## 1             286
## 2             288
## 3             285
## 4             283
## 5             277
## 6             263
str(pakistan_chelsa_dataframe)
## 'data.frame':    5400000 obs. of  4 variables:
##  $ CHELSA_bio10_12               : int  241 229 228 245 281 322 351 366 372 373 ...
##  $ merged_chelsa_annual_mean_temp: int  30 32 29 28 22 9 -5 -9 -14 -16 ...
##  $ CHELSA_bio10_18               : int  22 21 18 18 19 22 22 23 23 26 ...
##  $ CHELSA_bio10_10               : int  286 288 285 283 277 263 247 243 237 235 ...
## generate pseudo-absence data ####

pseudo_absence_pakistan <- randomPoints(cropped_chelsa_annual_global_prec, 18)
pseudo_absence_pakistan
##              x        y
##  [1,] 63.20403 28.56236
##  [2,] 79.57903 26.58736
##  [3,] 62.84569 36.02903
##  [4,] 82.61236 37.92903
##  [5,] 65.31236 37.78736
##  [6,] 74.10403 25.46236
##  [7,] 75.62069 36.87069
##  [8,] 69.55403 30.19569
##  [9,] 61.37903 33.82903
## [10,] 71.80403 37.81236
## [11,] 66.93736 30.90403
## [12,] 73.47903 25.32069
## [13,] 73.15403 30.28736
## [14,] 59.04569 32.39569
## [15,] 82.48736 34.08736
## [16,] 59.04569 26.85403
## [17,] 58.32903 26.76236
## [18,] 66.70403 32.13736
head(pseudo_absence_pakistan)
##             x        y
## [1,] 63.20403 28.56236
## [2,] 79.57903 26.58736
## [3,] 62.84569 36.02903
## [4,] 82.61236 37.92903
## [5,] 65.31236 37.78736
## [6,] 74.10403 25.46236
plot(pseudo_absence_pakistan)

# add column "0" for absence:

p_a <- 0

final_absence <- cbind(pseudo_absence_pakistan, p_a)
final_absence
##              x        y p_a
##  [1,] 63.20403 28.56236   0
##  [2,] 79.57903 26.58736   0
##  [3,] 62.84569 36.02903   0
##  [4,] 82.61236 37.92903   0
##  [5,] 65.31236 37.78736   0
##  [6,] 74.10403 25.46236   0
##  [7,] 75.62069 36.87069   0
##  [8,] 69.55403 30.19569   0
##  [9,] 61.37903 33.82903   0
## [10,] 71.80403 37.81236   0
## [11,] 66.93736 30.90403   0
## [12,] 73.47903 25.32069   0
## [13,] 73.15403 30.28736   0
## [14,] 59.04569 32.39569   0
## [15,] 82.48736 34.08736   0
## [16,] 59.04569 26.85403   0
## [17,] 58.32903 26.76236   0
## [18,] 66.70403 32.13736   0
# rename latlon columns

final_absence_df <- as.data.frame(final_absence)
final_absence_df
##           x        y p_a
## 1  63.20403 28.56236   0
## 2  79.57903 26.58736   0
## 3  62.84569 36.02903   0
## 4  82.61236 37.92903   0
## 5  65.31236 37.78736   0
## 6  74.10403 25.46236   0
## 7  75.62069 36.87069   0
## 8  69.55403 30.19569   0
## 9  61.37903 33.82903   0
## 10 71.80403 37.81236   0
## 11 66.93736 30.90403   0
## 12 73.47903 25.32069   0
## 13 73.15403 30.28736   0
## 14 59.04569 32.39569   0
## 15 82.48736 34.08736   0
## 16 59.04569 26.85403   0
## 17 58.32903 26.76236   0
## 18 66.70403 32.13736   0
colnames(final_absence_df)
## [1] "x"   "y"   "p_a"
names(final_absence_df)[1] <- "shp.x"
names(final_absence_df)[2] <- "shp.y"
final_absence_df
##       shp.x    shp.y p_a
## 1  63.20403 28.56236   0
## 2  79.57903 26.58736   0
## 3  62.84569 36.02903   0
## 4  82.61236 37.92903   0
## 5  65.31236 37.78736   0
## 6  74.10403 25.46236   0
## 7  75.62069 36.87069   0
## 8  69.55403 30.19569   0
## 9  61.37903 33.82903   0
## 10 71.80403 37.81236   0
## 11 66.93736 30.90403   0
## 12 73.47903 25.32069   0
## 13 73.15403 30.28736   0
## 14 59.04569 32.39569   0
## 15 82.48736 34.08736   0
## 16 59.04569 26.85403   0
## 17 58.32903 26.76236   0
## 18 66.70403 32.13736   0
## combine presence + absence ####

final_p_a_lat_lon <- rbind(final_presence, final_absence_df)
final_p_a_lat_lon
##       shp.x    shp.y p_a
## 53 74.75619 35.27489   1
## 54 74.25131 35.28893   1
## 55 72.38800 35.94713   1
## 56 71.90671 35.83228   1
## 57 72.02838 35.21013   1
## 58 71.88014 34.80043   1
## 59 71.90288 34.46425   1
## 60 73.43660 33.89194   1
## 61 73.09218 34.16677   1
## 62 72.90301 33.97403   1
## 63 73.09396 33.89551   1
## 64 72.61379 33.70444   1
## 65 72.81188 33.69195   1
## 66 74.17204 31.62093   1
## 67 73.99001 31.47994   1
## 68 66.75859 30.43480   1
## 69 66.72290 30.06361   1
## 70 67.00843 24.94896   1
## 71 73.06086 33.49902   1
## 1  63.20403 28.56236   0
## 2  79.57903 26.58736   0
## 3  62.84569 36.02903   0
## 4  82.61236 37.92903   0
## 5  65.31236 37.78736   0
## 6  74.10403 25.46236   0
## 7  75.62069 36.87069   0
## 8  69.55403 30.19569   0
## 9  61.37903 33.82903   0
## 10 71.80403 37.81236   0
## 11 66.93736 30.90403   0
## 12 73.47903 25.32069   0
## 13 73.15403 30.28736   0
## 14 59.04569 32.39569   0
## 15 82.48736 34.08736   0
## 16 59.04569 26.85403   0
## 17 58.32903 26.76236   0
## 18 66.70403 32.13736   0
## extract rastervalues of chelsa data for whole point data: ####
# only lonlat values for extracting:

final_lat_lon <- final_p_a_lat_lon[,1:2]
final_lat_lon
##       shp.x    shp.y
## 53 74.75619 35.27489
## 54 74.25131 35.28893
## 55 72.38800 35.94713
## 56 71.90671 35.83228
## 57 72.02838 35.21013
## 58 71.88014 34.80043
## 59 71.90288 34.46425
## 60 73.43660 33.89194
## 61 73.09218 34.16677
## 62 72.90301 33.97403
## 63 73.09396 33.89551
## 64 72.61379 33.70444
## 65 72.81188 33.69195
## 66 74.17204 31.62093
## 67 73.99001 31.47994
## 68 66.75859 30.43480
## 69 66.72290 30.06361
## 70 67.00843 24.94896
## 71 73.06086 33.49902
## 1  63.20403 28.56236
## 2  79.57903 26.58736
## 3  62.84569 36.02903
## 4  82.61236 37.92903
## 5  65.31236 37.78736
## 6  74.10403 25.46236
## 7  75.62069 36.87069
## 8  69.55403 30.19569
## 9  61.37903 33.82903
## 10 71.80403 37.81236
## 11 66.93736 30.90403
## 12 73.47903 25.32069
## 13 73.15403 30.28736
## 14 59.04569 32.39569
## 15 82.48736 34.08736
## 16 59.04569 26.85403
## 17 58.32903 26.76236
## 18 66.70403 32.13736
rasValue_pakistan_all <- extract(rasterstack_pakistan_chelsa, final_lat_lon)

rasValue_pakistan_all
##       CHELSA_bio10_12 merged_chelsa_annual_mean_temp CHELSA_bio10_18
##  [1,]             674                           -138              92
##  [2,]             407                           -107              68
##  [3,]             496                           -152              77
##  [4,]             639                           -120              64
##  [5,]            1293                            -10             480
##  [6,]            1320                             37             346
##  [7,]             689                            102             237
##  [8,]            2052                             14             881
##  [9,]            1726                             37             877
## [10,]            1060                            100             461
## [11,]            1677                             45             768
## [12,]             779                            104             102
## [13,]            1060                             96             530
## [14,]             589                            124              91
## [15,]             518                            126              83
## [16,]             212                             61              33
## [17,]             154                             56              35
## [18,]             203                            201              18
## [19,]             916                            105             134
## [20,]              42                            135               4
## [21,]             856                            150             110
## [22,]             313                             43               1
## [23,]              30                            -66              33
## [24,]             176                             45               0
## [25,]             503                            151             111
## [26,]             130                           -223              53
## [27,]             362                             78             161
## [28,]             219                             29               0
## [29,]             301                           -187              10
## [30,]             338                             -3              44
## [31,]             564                            165             126
## [32,]             240                            132              50
## [33,]             180                             28               0
## [34,]             146                           -183             124
## [35,]              93                            152               8
## [36,]             197                             83               6
## [37,]             313                             21              10
##       CHELSA_bio10_10
##  [1,]              91
##  [2,]             117
##  [3,]              74
##  [4,]             103
##  [5,]             192
##  [6,]             236
##  [7,]             299
##  [8,]             211
##  [9,]             233
## [10,]             297
## [11,]             243
## [12,]             307
## [13,]             297
## [14,]             330
## [15,]             333
## [16,]             302
## [17,]             292
## [18,]             309
## [19,]             305
## [20,]             368
## [21,]             336
## [22,]             293
## [23,]             266
## [24,]             320
## [25,]             316
## [26,]               3
## [27,]             286
## [28,]             282
## [29,]              52
## [30,]             236
## [31,]             326
## [32,]             342
## [33,]             268
## [34,]              47
## [35,]             361
## [36,]             273
## [37,]             286
## final training data ####

pakistan_butterflies_train_final_Gegenes_nostrodamus <- data.frame(final_p_a_lat_lon, rasValue_pakistan_all)
pakistan_butterflies_train_final_Gegenes_nostrodamus
##       shp.x    shp.y p_a CHELSA_bio10_12 merged_chelsa_annual_mean_temp
## 53 74.75619 35.27489   1             674                           -138
## 54 74.25131 35.28893   1             407                           -107
## 55 72.38800 35.94713   1             496                           -152
## 56 71.90671 35.83228   1             639                           -120
## 57 72.02838 35.21013   1            1293                            -10
## 58 71.88014 34.80043   1            1320                             37
## 59 71.90288 34.46425   1             689                            102
## 60 73.43660 33.89194   1            2052                             14
## 61 73.09218 34.16677   1            1726                             37
## 62 72.90301 33.97403   1            1060                            100
## 63 73.09396 33.89551   1            1677                             45
## 64 72.61379 33.70444   1             779                            104
## 65 72.81188 33.69195   1            1060                             96
## 66 74.17204 31.62093   1             589                            124
## 67 73.99001 31.47994   1             518                            126
## 68 66.75859 30.43480   1             212                             61
## 69 66.72290 30.06361   1             154                             56
## 70 67.00843 24.94896   1             203                            201
## 71 73.06086 33.49902   1             916                            105
## 1  63.20403 28.56236   0              42                            135
## 2  79.57903 26.58736   0             856                            150
## 3  62.84569 36.02903   0             313                             43
## 4  82.61236 37.92903   0              30                            -66
## 5  65.31236 37.78736   0             176                             45
## 6  74.10403 25.46236   0             503                            151
## 7  75.62069 36.87069   0             130                           -223
## 8  69.55403 30.19569   0             362                             78
## 9  61.37903 33.82903   0             219                             29
## 10 71.80403 37.81236   0             301                           -187
## 11 66.93736 30.90403   0             338                             -3
## 12 73.47903 25.32069   0             564                            165
## 13 73.15403 30.28736   0             240                            132
## 14 59.04569 32.39569   0             180                             28
## 15 82.48736 34.08736   0             146                           -183
## 16 59.04569 26.85403   0              93                            152
## 17 58.32903 26.76236   0             197                             83
## 18 66.70403 32.13736   0             313                             21
##    CHELSA_bio10_18 CHELSA_bio10_10
## 53              92              91
## 54              68             117
## 55              77              74
## 56              64             103
## 57             480             192
## 58             346             236
## 59             237             299
## 60             881             211
## 61             877             233
## 62             461             297
## 63             768             243
## 64             102             307
## 65             530             297
## 66              91             330
## 67              83             333
## 68              33             302
## 69              35             292
## 70              18             309
## 71             134             305
## 1                4             368
## 2              110             336
## 3                1             293
## 4               33             266
## 5                0             320
## 6              111             316
## 7               53               3
## 8              161             286
## 9                0             282
## 10              10              52
## 11              44             236
## 12             126             326
## 13              50             342
## 14               0             268
## 15             124              47
## 16               8             361
## 17               6             273
## 18              10             286